package com.fqcar.fqdf.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.bumptech.glide.Glide;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.core.BaseRequestFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author 张枭
 * @version V1.0
 * @date 2017/7/31
 * @desc
 */
public class MainSelectCarFragment extends BaseRequestFragment {
    @Override
    protected Unbinder getBinder() {
        return null;
    }

    /*private CarInfoRespBean carInfoRespBean;

    @BindView(R.id.select_car_item_charge)
    ImageView selectCarItemCharge;
    @BindView(R.id.select_car_item_distance)
    TextView selectCarItemDistance;
    @BindView(R.id.select_car_item_redpacket)
    TextView selectCarItemRedpacket;
    @BindView(R.id.select_car_item_favoured)
    TextView selectCarItemFavoured;
    @BindView(R.id.select_car_item_img)
    ImageView selectCarItemImg;
    @BindView(R.id.select_car_item_carversion)
    TextView selectCarItemCarversion;
    @BindView(R.id.select_car_item_seat)
    TextView selectCarItemSeat;
    @BindView(R.id.select_car_item_price)
    TextView selectCarItemPrice;
    @BindView(R.id.select_car_item_funaward)
    TextView selectCarItemFunaward;
    @BindView(R.id.select_car_item_funaward_llyt)
    LinearLayout selectCarItemFunawardLlyt;
    @BindView(R.id.select_car_item_num)
    TextView selectCarItemNum;
    private Unbinder bind;

    private String nightRuleUrl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.main_select_car_item, null);
        bind = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    protected Unbinder getBinder() {
        return bind;
    }

    private void initView() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            carInfoRespBean = arguments.getParcelable(Constants.BUNDLE_DATA);

            if (carInfoRespBean != null) {
                updateView(carInfoRespBean);

                getCarInfoV2(carInfoRespBean.getParkingIDs(), carInfoRespBean.getCarId());
            }
        }
        nightRuleUrl = arguments.getString(Constants.BUNDLE_DATA_EXT);

    }

    private void updateView(CarInfoRespBean carInfoRespBean) {
        int battery = carInfoRespBean.getBattery();

        if (battery >= 90) {
            selectCarItemCharge.setBackgroundResource(R.drawable.icon_battery_lv4);
        } else if (battery >= 70) {
            selectCarItemCharge.setBackgroundResource(R.drawable.icon_battery_lv3);
        } else if (battery >= 45) {
            selectCarItemCharge.setBackgroundResource(R.drawable.icon_battery_lv2);
        } else {
            selectCarItemCharge.setBackgroundResource(R.drawable.icon_battery_lv1);
        }

        selectCarItemNum.setText(carInfoRespBean.getPlateNum());

        selectCarItemDistance.setText(carInfoRespBean.getEnmileage());
        Glide.with(getContext()).load(carInfoRespBean.getCarImg()).fitCenter().into(selectCarItemImg);
        selectCarItemCarversion.setText(carInfoRespBean.getName());
        selectCarItemSeat.setText(carInfoRespBean.getSeats());

        selectCarItemFavoured.setVisibility(carInfoRespBean.getCarNightType() == Constants.NightTipText.isShow ? View.VISIBLE : View.GONE);

        String price = getContext().getResources().getString(R.string.using_car_select_price, carInfoRespBean.getPreTime()
                , carInfoRespBean.getPreMile());
        SpannableStringBuilder builder = new SpannableStringBuilder(price);
        changeTextColor(price, carInfoRespBean.getPreTime(), builder, R.color.n0db95f);
        changeTextColor(price, carInfoRespBean.getPreMile(), builder, R.color.n0db95f);

        selectCarItemPrice.setText(builder);

    }

    *//**
     * 修改text中的颜色值
     *
     * @param originText 原始text内容
     * @param changeText 要改变颜色的内容
     * @param builder    修改的对象引用
     * @param colorRes   颜色
     *//*
    private void changeTextColor(String originText, String changeText, SpannableStringBuilder builder, int colorRes) {
        if (CheckLogicUtil.isEmpty(changeText)) {
            return;
        }
        int preTimeStartIndex = originText.indexOf(changeText);
        if (preTimeStartIndex != -1) {
            int preTimeEndIndex = preTimeStartIndex + changeText.length();
            ForegroundColorSpan blueSpan = new ForegroundColorSpan(AndroidUtils.getColor(getContext(), colorRes));
            builder.setSpan(blueSpan, preTimeStartIndex, preTimeEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    }

    private void getCarInfoV2(String parkingID, String cardID) {
        RequestOptService.getReturnCarAmountV2(getActivity(), parkingID, cardID, new Response.Listener<NetBeanWrapper<CarAmountRespBean>>() {
            @Override
            public void onResponse(NetBeanWrapper<CarAmountRespBean> response) {


                if (handleBeanResponse(response)) {
                    return;
                }

                CarAmountRespBean carAmountRespBean = response.getModelData();

                if (!(isDetached() || isRemoving()) && selectCarItemRedpacket != null) {
                    if (CheckLogicUtil.isEmpty(carAmountRespBean.getRedPacketInfo())) {
                        selectCarItemRedpacket.setVisibility(View.GONE);
                    } else {
                        selectCarItemRedpacket.setVisibility(View.VISIBLE);
                        selectCarItemRedpacket.setText(carAmountRespBean.getRedPacketInfo());
                    }

                    if (carAmountRespBean.getParkingFeeRefunded() > 0) {

                        selectCarItemFunaward.setText(getString(R.string.using_car_select_funaward, carAmountRespBean.getParkingFeeRefunded()));
                        selectCarItemFunawardLlyt.setVisibility(View.VISIBLE);
                    } else {
                        selectCarItemFunawardLlyt.setVisibility(View.INVISIBLE);
                    }
                }
            }
        }, getDefaultErrorListener());
    }

    @OnClick({R.id.select_car_item_favoured, R.id.select_car_item_rule,R.id.select_car_item_redpacket})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_car_item_favoured:

                if (!CheckLogicUtil.isEmpty(nightRuleUrl)) {
                    Intent intent = new Intent(getContext(), WebActivity.class);
                    intent.putExtra("url", nightRuleUrl);
                    intent.putExtra(Constants.BUNDLE_DATA_EXT, true);
                    startActivity(intent);
                }

                break;
            case R.id.select_car_item_rule:
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("url", Constants.H5.FUNKAITINGJIANGLIGUIZE);
                startActivity(intent);
                break;
            case R.id.select_car_item_redpacket:
                Intent intentRed = new Intent(getContext(), WebActivity.class);
                intentRed.putExtra("url", Constants.H5.HONGBAOGUIZE);
                startActivity(intentRed);
                break;
            default:
                break;
        }
    }*/
}
